/*
 *  Copyright (C) 2011 JiangHongTiao <jjurco.sk@@@gmail.com>
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package sk.lieskove301.jianghongtiao.liaad.prisoner.statistics.charts;

import java.awt.Color;
import java.awt.Dimension;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
import sk.lieskove301.jianghongtiao.liaad.prisoner.gene.GeneticInfo;
import sk.lieskove301.jianghongtiao.liaad.prisoner.statistics.AgentsTimelineUnit;

/**
 * Date of create: May 15, 2011
 *
 * @author JiangHongTiao <jjurco.sk_gmail.com>
 * @version 2011.0515
 */
public class BestAgentsBarChart extends ApplicationFrame{

    private String title;
    private SelectStat stat;

    public BestAgentsBarChart(String title, SelectStat.FUNC_TYPE statF) {
        super(title);
        this.title = title;
        this.stat = SelectStat.getInstance(statF);
    }

    /**
     * Returns a sample dataset.
     * @param <Year, <Score, <GeneticInfo, AgentsTimelineUnit>>> agents
     * @return The dataset.
     */
    private CategoryDataset createDataset(Map<Integer, 
            Map<String, AgentsTimelineUnit>> agents) {
        
        // create the dataset...
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();
        for (Iterator<Integer> it = agents.keySet().iterator(); it.hasNext();) {
            Integer year = it.next();
            String category = year.toString();
            Map<String, AgentsTimelineUnit> yearAgents = agents.get(year);
            for (Iterator<String> it1 = yearAgents.keySet().iterator(); it1.hasNext();) {
                String geneticInfo = it1.next();
                if(yearAgents.containsKey(geneticInfo)){
                    dataset.addValue(stat.stat(yearAgents.get(geneticInfo.toString())),
                        geneticInfo.toString(), category);
                }
            }
        }
        return dataset;
    }

    /**
     * Creates a sample chart.
     *
     * @param dataset  the dataset.
     *
     * @return The chart.
     */
    private JFreeChart createChart(CategoryDataset dataset) {

        // create the chart...
        JFreeChart chart = ChartFactory.createStackedBarChart(
            title,       // chart title
            "Year",               // domain axis label
            "Value",                  // range axis label
            dataset,                  // data
            PlotOrientation.VERTICAL, // orientation
            true,                     // include legend
            true,                     // tooltips?
            false                     // URLs?
        );

        // NOW DO SOME OPTIONAL CUSTOMISATION OF THE CHART...

        // set the background color for the chart...
        chart.setBackgroundPaint(Color.white);

        // get a reference to the plot for further customisation...
        CategoryPlot plot = (CategoryPlot) chart.getPlot();

        // set the range axis to display integers only...
//        NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
//        rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());

        // disable bar outlines...
        BarRenderer renderer = (BarRenderer) plot.getRenderer();
        renderer.setDrawBarOutline(false);
        renderer.setShadowVisible(false);

//        CategoryAxis domainAxis = plot.getDomainAxis();
//        domainAxis.setCategoryLabelPositions(
//                CategoryLabelPositions.createUpRotationLabelPositions(
//                        Math.PI / 6.0));
        // OPTIONAL CUSTOMISATION COMPLETED.

        return chart;

    }

    public void main(Map<Integer, Map<String, AgentsTimelineUnit>> agents) {
        CategoryDataset dataset = createDataset(agents);
        JFreeChart chart = createChart(dataset);
        try {
            if(false)ChartUtilities.saveChartAsPNG(new File(title+".png"), chart, 3000, 5000);
        } catch (IOException ex) {
            Logger.getLogger(AbstractChart.class.getName()).
                    log(Level.SEVERE, null, ex);
        }
        ChartPanel chartPanel = new ChartPanel(chart);
        chartPanel.setFillZoomRectangle(true);
        chartPanel.setMouseWheelEnabled(true);
        chartPanel.setPreferredSize(new Dimension(500, 270));
        setContentPane(chartPanel);

        this.pack();
        RefineryUtilities.centerFrameOnScreen(this);
        this.setVisible(true);
    }
    
    public static BestAgentsBarChart showDiagram(String title,
            Map<Integer, Map<String, AgentsTimelineUnit>> agents,
            SelectStat.FUNC_TYPE statF){
        BestAgentsBarChart result = new BestAgentsBarChart(title, statF);
        result.main(agents);
        return result;
    }
}
